package com.wasion.gydpe.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @ClassName: BaseAction
 * @Description: 控制层Action的基类，提供基本方法和一般请求所需的request、response等
 * @author 高意
 * @date Sep 13, 2012 2:58:17 PM
 * 
 */
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 2801114348518113443L;

	private static final Logger logger = LoggerFactory
			.getLogger(BaseAction.class);

	public static final String EXCEPTION_MSG = "发生错误了，请联系管理员...";
	/*
	 * 请求所需的request、response
	 */
	// 获得当前的请求
	protected HttpServletRequest request = ServletActionContext.getRequest();
	// 获取当前页面响应
	protected HttpServletResponse response = ServletActionContext.getResponse();

	protected String rows;// 每页显示的记录数
	protected String page;// 当前第几页

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	/**
	 * 将数据输出至页面
	 * 
	 * @Title: printToView
	 * @Description: 将数据输出至页面
	 * @param result
	 * @throws IOException
	 */
	protected void printToView(String result) {
		try {
			response.setContentType("text/plain");
			response.setHeader("Content-Type", "text/html;charset=UTF-8");
			String cacheID = request.getParameter("cacheID");
			StringBuilder resultAll = new StringBuilder();
			resultAll.append("[");
			resultAll.append("{");
			resultAll.append("cacheID:\"" + cacheID + "\",");
			resultAll.append("cacheData:" + result);
			resultAll.append("}");
			resultAll.append("]");
			PrintWriter out = response.getWriter();
			response.addHeader("Access-Control-Allow-Origin", "*");
			out.print(resultAll);
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 将数据输出至页面
	 * 
	 * @Title: printToView
	 * @Description: 将数据输出至页面
	 * @param result
	 * @throws IOException
	 */
	protected void printToViewJustResult(String result) {
		try {
			response.setContentType("text/plain");
			response.setHeader("Content-Type", "text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			response.addHeader("Access-Control-Allow-Origin", "*");
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 
	 * @Title: get
	 * @Description: 转换参数，如果参数是长度为0的空串，将参数转换为null
	 * @param
	 * @return
	 * @throws
	 */
	protected Object get(Object object) {

		if (object instanceof String) {
			String temp = (String) object;
			if (temp.trim().length() == 0)
				return null;
		}

		return object;
	}
}
